<?php /* 	These code was generated using phpCIGen v 0.1.a (21/04/2009)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com,
    #songbee	mukhlisona@gmail.com
	#CV. Trust Solution, jl. Saronojiwo 19 Surabaya, http://www.ts.co.id

	+ Module  		: m_customer Model
	+ Description	: For record model process back-end
	+ Filename 		: c_m_customer.php
 	+ creator 		: 
 	+ Created on 06/Mar/2012 12:35:49

*/

class M_m_customer extends CI_Model{

		//constructor
		function M_m_customer() {
			parent::__construct();
		}

		//function for get list record
		function m_customer_list($filter,$start,$end){
			$sql = "SELECT customer_id,
							 customer_cp,
							 customer_hp,
							 customer_name,
							 customer_address,
							 customer_city,
							 customer_post,
							 customer_phone,
							 customer_fax,
							 customer_mail					  FROM 	 m_customer";

			// For simple search
			if ($filter<>""){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(customer_id) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(customer_cp) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(customer_hp) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(customer_name) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(customer_address) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(customer_city) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(customer_post) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(customer_phone) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(customer_fax) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(customer_mail) LIKE '%".addslashes(strtolower($filter))."%' )";
			}

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
                        $limit = $sql." LIMIT ".$start.",".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for create new record
		function m_customer_create($customer_cp,
				 $customer_hp,
				 $customer_name,
				 $customer_address,
				 $customer_city,
				 $customer_post,
				 $customer_phone,
				 $customer_fax,
				 $customer_mail){
			$data = array(
						 "customer_cp"=>$customer_cp,						 "customer_hp"=>$customer_hp,						 "customer_name"=>$customer_name,						 "customer_address"=>$customer_address,						 "customer_city"=>$customer_city,						 "customer_post"=>$customer_post,						 "customer_phone"=>$customer_phone,						 "customer_fax"=>$customer_fax,						 "customer_mail"=>$customer_mail					);
			$this->db->insert('m_customer', $data);
			if($this->db->affected_rows())
				return '1';
			else
				return '0';
		}

		//function for update record
		function m_customer_update($customer_id,
				 $customer_cp,
				 $customer_hp,
				 $customer_name,
				 $customer_address,
				 $customer_city,
				 $customer_post,
				 $customer_phone,
				 $customer_fax,
				 $customer_mail){
			$data = array(
						"customer_cp"=>$customer_cp,						"customer_hp"=>$customer_hp,						"customer_name"=>$customer_name,						"customer_address"=>$customer_address,						"customer_city"=>$customer_city,						"customer_post"=>$customer_post,						"customer_phone"=>$customer_phone,						"customer_fax"=>$customer_fax,						"customer_mail"=>$customer_mail					);

			$this->db->where('customer_id', $customer_id);
			$this->db->update('m_customer', $data);
			return '1';
		}

		//fcuntion for delete record
		function m_customer_delete($pkid){
			// You could do some checkups here and return '0' or other error consts.
			// Make a single query to delete all of the m_customers at the same time :
			if(sizeof($pkid)<1){
				return '0';
			} else if (sizeof($pkid) == 1){
				$sql = "DELETE FROM m_customer WHERE customer_id = ".$pkid[0];
				$this->db->query($sql);
			} else {
				$sql = "DELETE FROM m_customer WHERE ";
				for($i = 0; $i < sizeof($pkid); $i++){
					$sql = $sql . "customer_id= ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql = $sql . " OR ";
					}
				}
				$this->db->query($sql);
			}
			if($this->db->affected_rows()>0)
				return '1';
			else
				return '0';
		}

		//function for advanced search record
		function m_customer_search($customer_id,
				 $customer_cp,
				 $customer_hp,
				 $customer_name,
				 $customer_address,
				 $customer_city,
				 $customer_post,
				 $customer_phone,
				 $customer_fax,
				 $customer_mail,$start,$end){
			//full query
			$sql = "SELECT customer_id,
							 customer_cp,
							 customer_hp,
							 customer_name,
							 customer_address,
							 customer_city,
							 customer_post,
							 customer_phone,
							 customer_fax,
							 customer_mail					  FROM 	 m_customer";

			if($customer_id!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(customer_id) LIKE '%".addslashes(strtolower($customer_id))."%'";
			};
			if($customer_cp!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(customer_cp) LIKE '%".addslashes(strtolower($customer_cp))."%'";
			};
			if($customer_hp!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(customer_hp) LIKE '%".addslashes(strtolower($customer_hp))."%'";
			};
			if($customer_name!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(customer_name) LIKE '%".addslashes(strtolower($customer_name))."%'";
			};
			if($customer_address!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(customer_address) LIKE '%".addslashes(strtolower($customer_address))."%'";
			};
			if($customer_city!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(customer_city) LIKE '%".addslashes(strtolower($customer_city))."%'";
			};
			if($customer_post!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(customer_post) LIKE '%".addslashes(strtolower($customer_post))."%'";
			};
			if($customer_phone!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(customer_phone) LIKE '%".addslashes(strtolower($customer_phone))."%'";
			};
			if($customer_fax!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(customer_fax) LIKE '%".addslashes(strtolower($customer_fax))."%'";
			};
			if($customer_mail!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(customer_mail) LIKE '%".addslashes(strtolower($customer_mail))."%'";
			};

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
                        $limit = $sql." LIMIT ".$start.",".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for print record
		function m_customer_print($customer_id,
				 $customer_cp,
				 $customer_hp,
				 $customer_name,
				 $customer_address,
				 $customer_city,
				 $customer_post,
				 $customer_phone,
				 $customer_fax,
				 $customer_mail,$option,$filter){
			//full query
			$sql = "SELECT customer_id,
							 customer_cp,
							 customer_hp,
							 customer_name,
							 customer_address,
							 customer_city,
							 customer_post,
							 customer_phone,
							 customer_fax,
							 customer_mail					  FROM 	 m_customer";
			if($option=='LIST'){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(customer_id) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(customer_cp) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(customer_hp) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(customer_name) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(customer_address) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(customer_city) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(customer_post) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(customer_phone) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(customer_fax) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(customer_mail) LIKE '%".addslashes(strtoloer($filter))."%' )";
			} else if($option=='SEARCH'){
				if($customer_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_id) LIKE '%".addslashes(strtolower($customer_id))."%'";
				};
				if($customer_cp!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_cp) LIKE '%".addslashes(strtolower($customer_cp))."%'";
				};
				if($customer_hp!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_hp) LIKE '%".addslashes(strtolower($customer_hp))."%'";
				};
				if($customer_name!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_name) LIKE '%".addslashes(strtolower($customer_name))."%'";
				};
				if($customer_address!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_address) LIKE '%".addslashes(strtolower($customer_address))."%'";
				};
				if($customer_city!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_city) LIKE '%".addslashes(strtolower($customer_city))."%'";
				};
				if($customer_post!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_post) LIKE '%".addslashes(strtolower($customer_post))."%'";
				};
				if($customer_phone!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_phone) LIKE '%".addslashes(strtolower($customer_phone))."%'";
				};
				if($customer_fax!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_fax) LIKE '%".addslashes(strtolower($customer_fax))."%'";
				};
				if($customer_mail!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_mail) LIKE '%".addslashes(strtolower($customer_mail))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $query->result();
		}

		//function  for export to excel
		function m_customer_export_excel($customer_id ,$customer_cp ,$customer_hp ,$customer_name ,$customer_address ,$customer_city ,$customer_post ,$customer_phone ,$customer_fax ,$customer_mail ,$option,$filter){
			//full query
			$sql = "SELECT customer_id,
							 customer_cp,
							 customer_hp,
							 customer_name,
							 customer_address,
							 customer_city,
							 customer_post,
							 customer_phone,
							 customer_fax,
							 customer_mail					  FROM 	 m_customer";
			if($option=='LIST'){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= "(lower(customer_id) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(customer_cp) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(customer_hp) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(customer_name) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(customer_address) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(customer_city) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(customer_post) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(customer_phone) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(customer_fax) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(customer_mail) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				if($customer_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_id) LIKE '%".addslashes(strtolower($customer_id))."%'";
				};
				if($customer_cp!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_cp) LIKE '%".addslashes(strtolower($customer_cp))."%'";
				};
				if($customer_hp!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_hp) LIKE '%".addslashes(strtolower($customer_hp))."%'";
				};
				if($customer_name!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_name) LIKE '%".addslashes(strtolower($customer_name))."%'";
				};
				if($customer_address!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_address) LIKE '%".addslashes(strtolower($customer_address))."%'";
				};
				if($customer_city!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_city) LIKE '%".addslashes(strtolower($customer_city))."%'";
				};
				if($customer_post!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_post) LIKE '%".addslashes(strtolower($customer_post))."%'";
				};
				if($customer_phone!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_phone) LIKE '%".addslashes(strtolower($customer_phone))."%'";
				};
				if($customer_fax!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_fax) LIKE '%".addslashes(strtolower($customer_fax))."%'";
				};
				if($customer_mail!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(customer_mail) LIKE '%".addslashes(strtolower($customer_mail))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $sql;
		}

}
?>